package com.zry.yard.update;


import android.Manifest;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.FileProvider;

import com.tsy.sdk.myokhttp.response.DownloadResponseHandler;
import com.zry.yard.R;
import com.zry.yard.config.MyApplication;
import com.zry.yard.retrofit.BaseApiRetrofit;
import com.zry.yard.utils.ToastUtil;

import java.io.File;

/**
 * Created by zhangqie on 2017/6/16.
 */

public class UpdateUtils {
    private Context context;
    private MyApplication myApplication;
    private int id = 1;

    public UpdateUtils(Context context, MyApplication myApplication) {
        this.context = context;
        this.myApplication = myApplication;
    }

    public void showDownloadAPK(final String url, Notification.Builder mBuilder, NotificationManager mNotifyManager) {

        new BaseApiRetrofit().getMyOkHttp().download()
                .url(url)
                .filePath(myApplication.getApkSavePath() + "/追溯2.0.apk")
                .tag(context)
                .enqueue(new DownloadResponseHandler() {
                    @Override
                    public void onFinish(File downloadFile) {
                        mBuilder.setProgress(0, 0, false).setContentText("追溯2.0下载完成");

                        mNotifyManager.notify(id, mBuilder.build());
                        showSelectAPK();
                    }

                    @Override
                    public void onProgress(long currentBytes, long totalBytes) {
                        //更新进度条
                        int currentNum = (int) (100 * currentBytes / totalBytes);
                        mBuilder.setProgress(100, currentNum, false);
                        mNotifyManager.notify(id, mBuilder.build());
                    }

                    @Override
                    public void onFailure(String error_msg) {
                        mBuilder.setContentTitle("追溯2.0下载").setContentText("下载失败")
                                .setSmallIcon(R.mipmap.ic_launcher);
                    }
                });
    }


    /***
     * 调起安装APP窗口  安装APP
     */
    public void showSelectAPK() {
        File apkPath = new File(myApplication.getApkSavePath(), "追溯2.0.apk");
        if (Build.VERSION.SDK_INT >= 26) {
            //安装apk
            install(apkPath);
        } else {
            install(apkPath);
        }
    }


    public void install(File apkPath) {
        //7.0以上通过FileProvider
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            Uri apkUri = FileProvider.getUriForFile(context, "com.zry.yard.fileprovider", apkPath);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.startActivity(intent);
        } else {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(apkPath), "application/vnd.android" + ".package-archive");
            context.startActivity(intent);
        }
    }
}



